const RouterSystem = require("../template")

function testRouterSystemRouteManagement() {
  const router = new RouterSystem()

  console.log(
    "1. 路由守卫测试:",
    (() => {
      const component = { name: 'Guarded' }
      router.register('/guarded', component)
      
      let guardCalled = false
      const guard = (to, from, next) => {
        guardCalled = true
        next()
      }
      
      router.addGuard(guard, 'before')
      
      return new Promise((resolve) => {
        router.navigate('/guarded').then(() => {
          resolve(guardCalled === true)
        })
      })
    })()
  )

  console.log(
    "2. 路由中间件测试:",
    (() => {
      const component = { name: 'Middleware' }
      router.register('/middleware', component)
      
      let middlewareCalled = false
      const middleware = (to, from, next) => {
        middlewareCalled = true
        next()
      }
      
      router.addMiddleware(middleware)
      
      return new Promise((resolve) => {
        router.navigate('/middleware').then(() => {
          resolve(middlewareCalled === true)
        })
      })
    })()
  )

  console.log(
    "3. 路由守卫阻止测试:",
    (() => {
      const component = { name: 'Blocked' }
      router.register('/blocked', component)
      
      const guard = (to, from, next) => {
        next(false) // 阻止导航
      }
      
      router.addGuard(guard, 'before')
      
      return new Promise((resolve) => {
        router.navigate('/blocked').then((success) => {
          resolve(success === false)
        })
      })
    })()
  )

  console.log(
    "4. 路由守卫重定向测试:",
    (() => {
      const component1 = { name: 'RedirectFrom' }
      const component2 = { name: 'RedirectTo' }
      
      router.register('/redirect-from', component1)
      router.register('/redirect-to', component2)
      
      const guard = (to, from, next) => {
        if (to.path === '/redirect-from') {
          next('/redirect-to')
        } else {
          next()
        }
      }
      
      router.addGuard(guard, 'before')
      
      return new Promise((resolve) => {
        router.navigate('/redirect-from').then(() => {
          const currentRoute = router.getCurrentRoute()
          resolve(currentRoute.path === '/redirect-to')
        })
      })
    })()
  )

  console.log(
    "5. 路由参数测试:",
    (() => {
      const component = { name: 'Param' }
      router.register('/user/:id', component)
      
      return new Promise((resolve) => {
        router.navigate('/user/123').then(() => {
          const currentRoute = router.getCurrentRoute()
          resolve(currentRoute.params && currentRoute.params.id === '123')
        })
      })
    })()
  )

  console.log(
    "6. 路由查询参数测试:",
    (() => {
      const component = { name: 'Query' }
      router.register('/search', component)
      
      return new Promise((resolve) => {
        router.navigate('/search?q=test&page=1').then(() => {
          const currentRoute = router.getCurrentRoute()
          resolve(currentRoute.query && 
                 currentRoute.query.q === 'test' && 
                 currentRoute.query.page === '1')
        })
      })
    })()
  )

  console.log(
    "7. 路由嵌套测试:",
    (() => {
      const parentComponent = { name: 'Parent' }
      const childComponent = { name: 'Child' }
      
      router.register('/parent', parentComponent)
      router.register('/parent/child', childComponent)
      
      return new Promise((resolve) => {
        router.navigate('/parent/child').then(() => {
          const currentRoute = router.getCurrentRoute()
          resolve(currentRoute.path === '/parent/child')
        })
      })
    })()
  )

  console.log(
    "8. 路由通配符测试:",
    (() => {
      const component = { name: 'Wildcard' }
      router.register('/wildcard/*', component)
      
      return new Promise((resolve) => {
        router.navigate('/wildcard/any/path').then(() => {
          const currentRoute = router.getCurrentRoute()
          resolve(currentRoute.path === '/wildcard/any/path')
        })
      })
    })()
  )

  console.log(
    "9. 路由守卫移除测试:",
    (() => {
      const component = { name: 'RemoveGuard' }
      router.register('/remove-guard', component)
      
      let guardCalled = false
      const guard = (to, from, next) => {
        guardCalled = true
        next()
      }
      
      router.addGuard(guard, 'before')
      router.removeGuard(guard, 'before')
      
      return new Promise((resolve) => {
        router.navigate('/remove-guard').then(() => {
          resolve(guardCalled === false)
        })
      })
    })()
  )

  console.log(
    "10. 路由中间件移除测试:",
    (() => {
      const component = { name: 'RemoveMiddleware' }
      router.register('/remove-middleware', component)
      
      let middlewareCalled = false
      const middleware = (to, from, next) => {
        middlewareCalled = true
        next()
      }
      
      router.addMiddleware(middleware)
      router.removeMiddleware(middleware)
      
      return new Promise((resolve) => {
        router.navigate('/remove-middleware').then(() => {
          resolve(middlewareCalled === false)
        })
      })
    })()
  )

  console.log(
    "11. this上下文路由管理测试:",
    (() => {
      const routerMgrObj = {
        multiplier: 2,
        processRoute: function(path) {
          const router = new RouterSystem()
          const component = { name: 'Test' }
          router.register(path, component)
          return router.hasRoute(path) ? this.multiplier : 0
        }
      }
      return routerMgrObj.processRoute('/test') === 2
    })()
  )

  console.log(
    "12. 复杂对象this绑定路由管理测试:",
    (() => {
      const routerMgrConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(path) {
          const router = new RouterSystem()
          const component = { name: 'Test' }
          router.register(path, component)
          return router.hasRoute(path) && path.length >= this.config.threshold
        }
      }
      return routerMgrConfigObj.processWithConfig('/test') === true
    })()
  )
}

testRouterSystemRouteManagement()
